#!/user/bin/env node

const fs = require('fs'),
      join = require('path').join,
      log = console.log,
      err = console.error,
      exit = process.exit,
      file = process.argv[2];

if(process.argv.length === 2){
  console.error('请输入参数！');
  exit(1);
}else if(!fs.existsSync(file)){
  console.error(`${file} not exixt!`);
  exit(2);
}

if(fs.statSync(file).isFile()){
  fs.unlinkSync(file);
  log('删除文件成功');
  exit(3);
}else if(fs.statSync(file).isDirectory()){
  deleteDir(file);
  log('删除目录成功！');
  exit(4);
}

function deleteDir(floder){
  let files = fs.readdirSync(floder);

  for(let i=0;i<files.length;i++){
    let file = join(floder,files[i]);

    if(fs.statSync(file).isFile()){
     fs.unlinkSync(file);
     continue;
    }
    if(fs.statSync(file).isDirectory){
      deleteDir(file);
    }
    fs.rmdirSync(floder);
}
}

